<template>
	<view>
		<!-- 简约风格 -->
		<!-- <kz-page-my-simple :user="user" @login="login" @goTo="goTo" @clearStorage="clearStorage" v-if="pageStyle == 'simple'"></kz-page-my-simple> -->
		<kz-page-my-simple :user="user" @login="login" @goTo="goTo" @clearStorage="clearStorage" ></kz-page-my-simple>
		
		<!-- 多彩风格 -->
 		<!-- <kz-page-my-color :user="user" @login="login" @goTo="goTo" @clearStorage="clearStorage" v-else-if="pageStyle == 'color'"></kz-page-my-color> -->

		<!-- 流量主组件 -->
		<!-- #ifdef MP-WEIXIN -->
		<kz-ad ref="adMy" kind="BANNER" :config="ad" field="my_banner"></kz-ad>
		<kz-ad ref="adMy" kind="VIDEO" :config="ad" field="my_video"></kz-ad>
		<kz-ad ref="adMy" kind="VIDEO_PATCH" :config="ad" field="my_video_patch"></kz-ad>
		<!-- #endif -->

		<!-- 大嘴鸟组件 -->
		<!-- <monster v-if="showMonster"></monster> -->

		<!-- <login ref="login" v-on:succ="ajax"></login> -->
		<!-- <tabbar></tabbar> -->
		<bastTabbar :tabbar_index="3"></bastTabbar>
	</view>
</template>

<script>
import userApi from "@/exam/common/api/user.js"
import kzPageMySimple from '@/exam/components/kz-page-my-simple/kz-page-my-simple.vue';
import kzPageMyColor from '@/exam/components/kz-page-my-color/kz-page-my-color.vue';
import kzAd from '@/exam/components/kz-ad/kz-ad.vue';
import bastTabbar from '@/component/base-tabbar.vue'
import login from '@/exam/components/login/login.vue';

import { nextTick } from "vue";

	export default {
		components: {
			bastTabbar,
			kzPageMySimple,
			kzPageMyColor,
			kzAd,
			login
		},
		data() {
			return {
				imgUrl: this.url,
				user: null,
				ad: {},
				pageStyle: 'simple',
				showMonster: false,
				isLogin : false

			};
		},
		onLoad() {

		},
		onShow() {
			uni.$on('login_success', (data) => {
				console.log('login_success', data)
				this.user = data.user
				this.ajax()
			})
			this.ajax()
			if(uni.getStorageSync('token') || uni.getStorageSync("userInfo").token){
				this.isLogin = true
			}else{

			this.$nextTick(()=>{
				 console.log(this.$refs.login)
				 this.$refs.login.modal = true;
			})

			}
		},
		onHide() {
			uni.$off('login_success')
		},
		methods: {
			ajax() {
				// 获取配置
				this.getSetting()
				
				if (uni.getStorageSync('token') || uni.getStorageSync("userInfo").token) {
					userApi.getUserInfo(this).then(res => {
						if (res.code == 1) {
							this.user = res.data
							uni.setStorageSync('user', this.user)
						} else {
							uni.$emit('uniLogin')
						}
					})
				} else {
					uni.$emit('uniLogin')
				}

				// setTimeout(() => {
				// 	this.user = uni.getStorageSync('user')
				// 	console.log('user', this.user)
				// }, 500)
			},
			
			loadSetting() {
				let page = this.utils.getData('page')
				console.log('page = ', page);
				if (page) {
					// 页面风格
					this.pageStyle = page.page_my_style ? page.page_my_style : 'color2'
					// 大嘴鸟
					this.showMonster = page.page_my_monster_btn == 1
				}
				
				// #ifdef MP-WEIXIN
				this.ad = this.utils.getData('ad')
				// 插屏广告
				if (this.ad.my_cp_open == 1) {
					this.adUtils.interstitial.load(this.ad.my_cp)
					this.adUtils.interstitial.show()
				}
				// #endif
			},
			
			getSetting() {
				this.http('common/index', '', 'get').then(res => {
					if (!res.code) {
						uni.showToast({
							title: '获取数据失败，请刷新重试',
							icon: 'error'
						})
						return
					}
				
					// 系统配置数据
					let system = res.data.system;
					console.log('system = ', system);
					if (system) {
						this.system = system
						uni.setStorageSync('system', system)
					}
				
					// 页面配置数据
					let page = res.data.page
					if (page) {
						this.page = page
						uni.setStorageSync('page', page)
					}
					
					// #ifdef MP-WEIXIN
					// 流量主数据
					let ad = res.data.ad;
					if (ad) {
						this.ad = ad
						uni.setStorageSync('ad', ad);
					
						// 插屏广告
						if (this.ad.index_cp_open == 1) {
							this.adUtils.interstitial.load(this.ad.index_cp)
							this.adUtils.interstitial.show()
						}
					}
					// #endif
					
					this.loadSetting();
					
					console.log('pageStyle = ', this.pageStyle);
				})
			},
			
			login() {
				console.log('on login')
				if (!uni.getStorageSync('token') && !(uni.getStorageSync("userInfo").token)) {
					this.$refs.login.modal = true;
					return;
				} else {
					uni.navigateTo({
						url: 'set'
					});
				}
			},
			goTo(page) {
				console.log('page', page)
				if (!uni.getStorageSync('token') && !(uni.getStorageSync("userInfo").token)) {
					this.$refs.login.modal = true;
					return;
				} else {
					uni.navigateTo({
						url: page
						// url: e.currentTarget.dataset.url
					});
				}
			},
			clearStorage() {
				console.log('on clearStorage')
				uni.showModal({
					title: '提醒',
					content: '是否确认要清理缓存吗？（将清理用户登录信息及练题记忆模式等数据）',
					confirmColor: '#4A90E2',
					success: (t) => {
						t.confirm ?
							(uni.showLoading({
									title: '正在清除缓存...',
									mask: !0
								}),
								uni.clearStorage({
									success: () => {
										this.user = null
										uni.setStorageSync('user', '');
										uni.setStorageSync('user_id', '');
										uni.setStorageSync('openid', '');
									},
									fail: (t) => {
										console.log('fail');
									},
									complete: function(t) {
										setTimeout(function() {
											uni.hideLoading();
										}, 1000);
									}
								}),
								this.getSetting()
							) :
							t.cancel && console.log('取消');
					}
				});
			},
			tel() {
				uni.makePhoneCall({
					phoneNumber: this.copyright_mobile
				});
			}
		}
	};
</script>

<style>
	page {
		/* background-color: #fff; */
	}


</style>
